package leo.game.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import leo.data.GameConst;
	import leo.data.GameModel;
	import leo.data.TowerVO;
	import leo.game.stage.Game;
	import leo.game.ui.UIMap;
	import leo.manager.BitmapManager;
	import leo.manager.McMannager;

	/**
	 * 防守塔 
	 * @author leo
	 * 
	 */
	public class BaseTower extends MapObject
	{
		
		/**
		 * 位于地图上的那一行 
		 */		
		public var mapX :int;
		/**
		 * 位于地图上的那一列
		 */		
		public var mapY :int;
		/**
		 * uid值 
		 */		
		public var uid :String;
		/**
		 * 是否存活 
		 */		
		public var isAlive :Boolean;
		/**
		 * 鼠标On 
		 */		
		public var mouseOn :Boolean;
		/**
		 * 使用的位图 
		 */		
		public var bitmap :Bitmap;
		/**
		 * 防守塔的数据 
		 */		
		public var towerVO :TowerVO;
		/**
		 * 子弹出生点 
		 */		
		public var bulletStartPoint :Point;
		/**
		 * 子弹刷新时间 
		 */		
		private var iterval :int;
		/**
		 * 防守塔的攻击目标 
		 */		
		private var targetAry :Array;
		/**
		 * 防守塔底座 
		 */		
		private var baseBitmap :Bitmap;
		/**
		 * 碰撞检测矩形 
		 */		
		private var rect :Rectangle;
		public function BaseTower(vo :TowerVO)
		{
			this.towerVO = vo;
			this.isAlive = true;
			this.uid = "t" + int(1000 * Math.random());
			bulletStartPoint = new Point();
			this.targetAry = [];
			
			var bmd :BitmapData = BitmapManager.instance.getBitmapdataByName(GameConst.BASE);
			this.baseBitmap = new Bitmap(bmd);
			this.baseBitmap.visible = false;
			this.baseBitmap.x = -10;
			this.baseBitmap.y = -10;
			this.addChild(baseBitmap);
			
			this.bitmap = new Bitmap(towerVO.bitmapData);
			this.addChild(bitmap);
			
			
		}
		
		override public function update():void
		{
			var monsterList :Vector.<BaseMonster> = GameModel.instance.monsterList;
			if(monsterList.length > 0)
			{
				var index :int = 0;
				for each(var monster :BaseMonster in monsterList)
				{
					if(monster.visible && monster.isAlive)
					{
						//小于攻击范围就发动攻击
						var mx :Number = monster.x + monster.width/2;
						var my :Number = monster.y + monster.height/2;
						var mp :Point =  new Point(mx,my);
//						var mp :Point =  getMonsterAttPoint(monster);
						var tx :Number = this.x + this.width/2;
						var ty :Number = this.y + this.height/2;
						var tp :Point = new Point(tx ,ty);
						if(Point.distance(tp,mp) < this.towerVO.attRange)
						{
							//群攻塔同时追踪多个目标
							var num :int = this.towerVO.attTargetNum;
							if(index < num)
							{
								this.targetAry[index] = monster;
								index++;
							}
						}else
						{
							for(var i :int = this.targetAry.length - 1; i >= 0; i--)
							{
								var m :BaseMonster = this.targetAry[i] as BaseMonster;
								if(m.uid == monster.uid)
								{
									//超出范围清空目标列表
									this.targetAry.splice(i,1);
								}
								index = 0;
							}
						}
					}
				}
				//显示子弹创建的速度
				this.iterval += Game.instance.refreshInterval;

				if(this.iterval >= this.towerVO.attSpeed)
				{
					for(var bulletNum :int = 0; bulletNum < this.targetAry.length; bulletNum++)
					{
						if(this.targetAry[bulletNum].isAlive && this.targetAry[bulletNum].visible)
						{
							var name :String = this.towerVO.id;
							name = changeName(name);
							var bullet :Bullet = new Bullet(name,this,this.targetAry[bulletNum],this.bulletStartPoint.x,this.bulletStartPoint.y);
							UIMap.instance.addItem(bullet);
							this.iterval = 0;
						}
					}
				}
			}
			
		}

		
		public function tooltip():*
		{
			return null;
		}
		/**
		 * 判断鼠标是否在防守塔上 
		 * @return 
		 * 
		 */		
		public function isMouseOn():Boolean
		{
			var result :Boolean = false;
			var rect :Rectangle = getBounds(this);
			if(rect.contains(this.mouseX,this.mouseY))
			{
				result = true;
			}else
			{
				result = false;
			}
			return result;
		}
		/**
		 * 显示防守塔底座 
		 * 
		 */		
		public function showBaseRect():void
		{
			this.baseBitmap.visible = true;
		}
		/**
		 * 銷毀
		 */		
		public function destroy():void
		{
			
		}
		/**
		 * 根据怪物位置获取较佳的怪物攻击点 
		 * @param monster
		 * @return 
		 * 
		 */		
		private function getMonsterAttPoint(monster :BaseMonster):Point
		{
			var point :Point;
			if(this.x < monster.x && this.y < monster.y) 
			{
				point = new Point(monster.x,monster.y);  //左上角
			}else if(this.x < monster.x  && this.y > monster.y)
			{
				point = new Point(monster.x,monster.y + monster.height);  //左下角
			}else  if(this.x > monster.x && this.y < monster.y)
			{
				point = new Point(monster.x + monster.width,monster.y);  //右上角
			}else  if(this.x > monster.x && this.y > monster.y)
			{
				point = new Point(monster.x + monster.width, monster.y + monster.height);  //右下角
			}else
			{
				point = new Point(monster.x,monster.y);
			}
			return point;
		}
		
		
		/**
		 * 转换名字，将塔的id转成子弹的链接名 
		 * @param name
		 * @return 
		 * 
		 */		
		private function changeName(name :String):String
		{
			var result :String;
			var index :int = name.indexOf("Tower");
			result = name.slice(0,index) + "Bullet";
			return result;
		}
		
		
	}
}